package com.young.servicehosp.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.api.R;
import com.baomidou.mybatisplus.extension.conditions.update.LambdaUpdateChainWrapper;
import com.baomidou.mybatisplus.extension.conditions.update.UpdateChainWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.google.common.collect.Maps;
import com.young.commonutil.result.Result;
import com.young.model.enums.HospitalSetStatusEnum;
import com.young.model.model.hosp.HospitalSet;
import com.young.model.vo.hosp.HospitalSetQueryVo;
import com.young.servicehosp.service.HospitalSetService;
import com.young.servicehosp.service.impl.HospitalSetServiceImpl;
import com.young.serviceutil.utils.MD5;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;
import java.util.Random;

@RestController
@RequestMapping("/admin/hosp/hospitalSet")
public class HospitalSetController {

    @Autowired
    private HospitalSetService hospitalSetService;


    @ApiOperation(value = "获取所有医院设置")
    @GetMapping("findAll")
    public Result<List<HospitalSet>> findAllHospitalSet() {
        List<HospitalSet> hospitalSetList = hospitalSetService.getHospitalSetList();
        return Result.ok(hospitalSetList);
    }

    //2 逻辑删除医院设置
    @ApiOperation(value = "逻辑删除医院设置")
    @DeleteMapping("{id}")
    public Result removeById(@PathVariable Long id) {
        boolean b = hospitalSetService.removeById(id);
        if (b) {
            HospitalSetServiceImpl.hospitalSetListMap.clear();
            HospitalSetServiceImpl.hospitalSetPageMap.clear();
            return Result.ok();
        } else {
            return Result.fail();
        }
    }

    @ApiOperation(value = "条件查询带分页")
    @PostMapping("findPageHospSet/{current}/{limit}")
    public Result<Page<HospitalSet>> findPageHospSet(@PathVariable long current,
                                                     @PathVariable long limit,
                                                     @RequestBody(required = false) HospitalSetQueryVo hospitalSetQueryVo) {

        Page<HospitalSet> page = new Page<>(current, limit);
        LambdaQueryWrapper<HospitalSet> wrapper = new LambdaQueryWrapper<HospitalSet>().orderByDesc(HospitalSet::getCreateTime);

        if (hospitalSetQueryVo != null) {
            wrapper.like(StringUtils.isNotBlank(hospitalSetQueryVo.getHoscode()), HospitalSet::getHoscode, hospitalSetQueryVo.getHoscode())
                    .like(StringUtils.isNotBlank(hospitalSetQueryVo.getHosname()), HospitalSet::getHosname, hospitalSetQueryVo.getHosname());
        }
        Page<HospitalSet> hospitalSetPage = hospitalSetService.pageHospSet(page, wrapper);

        return Result.ok(hospitalSetPage);
    }

    @ApiOperation(value = "添加医院设置")
    @PostMapping("saveHospitalSet")
    public Result saveHospitalSet(@RequestBody HospitalSet hospitalSet) {
        hospitalSet.setStatus(HospitalSetStatusEnum.CAN_USE.getCode());
        hospitalSet.setSignKey(MD5.encrypt(System.currentTimeMillis() + "" + new Random(1000)));
        hospitalSetService.save(hospitalSet);
        HospitalSetServiceImpl.hospitalSetListMap.clear();
        HospitalSetServiceImpl.hospitalSetPageMap.clear();
        return Result.ok();
    }

    @ApiOperation(value = "根据id获取医院设置")
    @GetMapping("getHospSet/{id}")
    public Result<HospitalSet> getHospSet(@PathVariable Long id) {
        HospitalSet byId = hospitalSetService.getById(id);
        return Result.ok(byId);
    }

    @ApiOperation(value = "修改医院设置")
    @PostMapping("updateHospitalSet")
    public Result updateHospitalSet(@RequestBody HospitalSet hospitalSet) {
        hospitalSetService.updateById(hospitalSet);
        HospitalSetServiceImpl.hospitalSetListMap.clear();
        HospitalSetServiceImpl.hospitalSetPageMap.clear();
        return Result.ok();
    }


    @ApiOperation(value = "批量删除医院设置")
    @DeleteMapping("batchRemove")
    public Result batchRemoveHospitalSet(@RequestBody List<Long> idList) {
        hospitalSetService.removeByIds(idList);
        HospitalSetServiceImpl.hospitalSetListMap.clear();
        HospitalSetServiceImpl.hospitalSetPageMap.clear();
        return Result.ok();
    }

    //8 医院设置锁定和解锁
    @ApiOperation(value = "医院设置锁定和解锁")
    @PutMapping("lockHospitalSet/{id}/{status}")
    public Result lockHospitalSet(@PathVariable Long id,
                                  @PathVariable Integer status) {
        LambdaUpdateWrapper<HospitalSet> lambdaUpdateWrapper = new LambdaUpdateWrapper<HospitalSet>().set(HospitalSet::getStatus, status).eq(HospitalSet::getId, id);
        hospitalSetService.update(lambdaUpdateWrapper);
        HospitalSetServiceImpl.hospitalSetListMap.clear();
        HospitalSetServiceImpl.hospitalSetPageMap.clear();
        return Result.ok();
    }

    @GetMapping("testException")
    public Result testException() {
        int i = 10 / 0;
        return Result.ok();
    }

}
